package com.wlw.eye.ui

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.widget.LinearLayoutManager
import com.gyf.barlibrary.ImmersionBar
import com.wlw.eye.R
import com.wlw.eye.adapter.DownloadAdapter
import com.wlw.eye.mvp.model.bean.VideoBean
import com.wlw.eye.utils.ObjectSaveUtils
import com.wlw.eye.utils.SPUtils
import kotlinx.android.synthetic.main.activity_cache.*
import org.jetbrains.anko.async
import org.jetbrains.anko.uiThread
import zlc.season.rxdownload2.RxDownload

class CacheActivity : AppCompatActivity() {
    var mList = ArrayList<VideoBean>()
    lateinit var mAdapter: DownloadAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImmersionBar.with(this).transparentBar().barAlpha(0.3f).fitsSystemWindows(true).init()
        setContentView(R.layout.activity_cache)
        setToolbar()
        recyclerView.layoutManager = LinearLayoutManager(this)
        mAdapter = DownloadAdapter(mList)
        mAdapter.setOnLongClickListener(object : DownloadAdapter.OnLongClickListener {
            override fun onLongClick(position: Int) {
                addDialog(position)
            }
        })
        recyclerView.adapter = mAdapter
        initData()
    }

    private fun addDialog(position: Int) {
        val builder = AlertDialog.Builder(this)
        builder.setMessage("是否删除当前视频")
                .setNegativeButton("否", { dialog, _ -> dialog.dismiss() })
                .setPositiveButton("是", { _, _ -> deleteDownload(position) })
                .create()
                .show()
    }

    private fun deleteDownload(position: Int) {
        RxDownload.getInstance(this).deleteServiceDownload(mList[position].playUrl, true).subscribe()
        SPUtils.getInstance(this, "downloads").put(mList[position].playUrl!!, "")
        val count = position + 1
        ObjectSaveUtils.deleteFile("download$count", this)
        mList.removeAt(position)
        mAdapter.notifyItemRemoved(position)
    }

    private fun initData() {
        async {
            val list = ArrayList<VideoBean>()
            var count = SPUtils.getInstance(this@CacheActivity, "downloads").getInt("count")
            var i = 1
            while (i <= count) {
                var bean: VideoBean
                if (ObjectSaveUtils.getValue(this@CacheActivity, "download$i") == null)
                    continue
                else
                    bean = ObjectSaveUtils.getValue(this@CacheActivity, "download$i") as VideoBean
                list.add(bean)
                i++
            }
            uiThread {
                mList.addAll(list)
                mAdapter.notifyDataSetChanged()
            }
        }
    }

    private fun setToolbar() {
        setSupportActionBar(toolbar)
        var bar = supportActionBar
        bar?.title = "我的缓存"
        bar?.setDisplayHomeAsUpEnabled(true)
        toolbar.setNavigationOnClickListener { onBackPressed() }
    }
}
